const db = wx.cloud.database()
const teamlistCollection = db.collection('teamlist')
const util = require('../../utils/util')
//Page Object
Page({
  data: {
    openid: '',
    nickname: '',
    mypublishList: [],
    showmodal: false,
    tablist: ['我发布的', '我加入的', '我申请的'],
    currenttabindex: 0,
    showApply: true
  },
  //options(Object)
  onLoad: function (options) {
    this.getmyTeam()
  },
  onReady: function () {
    this.getmypublish()
  },
  getmypublish: function () {
    wx.showLoading({
      title: '数据加载中……',
    })
    wx.cloud.init({
      env: 'cloud1-5glocnmp62950062'
    })
    wx.cloud.callFunction({
      name: 'getmypublish',
      complete: res => {
        // let mypublishList = res.result.teamlistResult.list
        let mypublishList = res.result.teamlistResult.list.map((item) => {
          let date = util.formatTime(new Date(item.date))
          item.date = date
          return item
        }).reverse()
        this.setData({
          mypublishList: mypublishList.reverse()
        })
        wx.hideLoading()
      }
    })
  },
  getmyTeam: function () {
    this.setData({
      openid: wx.getStorageSync('OPENID'),
    })
    wx.showLoading({
      title: '数据加载中……',
    })
    let that = this
    this.setData({
      openid: wx.getStorageSync('OPENID'),
    })
    wx.cloud.init({
      env: 'cloud1-5glocnmp62950062'
    })
    wx.cloud.callFunction({
      name: 'getmyTeam',
      success: function (res) {
        let myteamList = res.result.myjoinTeamResult.data.map((item) => {
          let date = util.formatTime(new Date(item.date))
          item.date = date
          return item
        }).reverse()
        that.setData({
          myteamList: myteamList,
        })
        console.log(res)
        wx.hideLoading({
          success: (res) => {},
        })
      },
      fail: function () {
        wx.showToast({
          title: '获取数据失败，请稍后再试',
          icon: 'none',
        })
      },
      complete: res => {}
    })
    wx.cloud.callFunction({
      name: 'getapplylist',
      complete: res => {
        console.log('callFunction test result:', res)
        let applyinglist = res.result.applicationResult.list.filter(
          item => item._openid == this.data.openid
          ).map(item => {
            item.applicationlist[0].appid = item._id
          return item.applicationlist[0]
        })
        applyinglist = applyinglist.map((item) => {
          let date = util.formatTime(new Date(item.date))
          item.date = date
          return item
        }).reverse()
        console.log('applicationResult:', applyinglist)
        this.setData({
          applyinglist: applyinglist
        })
      }
    })
  },

  showapplication: function () {
    this.setData({
      showModal: true
    })
  },
  changetab: function (e) {
    let index = e.currentTarget.dataset.index
    this.setData({
      currenttabindex: index,
      showApply: index == 0
    })
  },
  onPullDownRefresh: function () {
    this.getmypublish()
    this.getmyTeam()
    wx.stopPullDownRefresh()
  }
});